<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath%>">
    <meta charset="UTF-8">
    <title>运动康复方案增加或者修改页面</title>

    <link rel="stylesheet" href="../res/layui/css/layui.css" media="all">

    <script type="text/javascript" src="../res/js/jquery-3.4.1.min.js"></script>
    <script src="../res/layui/layui.js"></script>
    <style type="text/css">
        #main {
            margin: 10px;
        }

        body {
            padding-top: 5px;
        }
    </style>

    <script type="text/javascript">
        //全局同步
        $.ajaxSetup({
            async: false
        });
        function getQueryString(name){
            var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if(r!=null)return  unescape(r[2]); return null;}
        var id = getQueryString("id");
       /* alert(id);*/

        $(function(){
          $("#saId").val(id);//和模型类一样


            /**
             批量删除
             */
            $("#btnDelete").on("click",function(){
                var rolesId = "";
                $("input[name='roles']:checked").each(function(index,item){

                    rolesId+=$(this).val()+",";

                });

                if(rolesId==""){
                    alert("请选择一行记录!");
                    return ;
                }

                if(confirm("确定需要删除吗?")){
                    var url = "RolesContiller/select.action";
                    var data ={rolesId:rolesId};
                    $.post(url,data,function(obj){
                        alert(rolesId);
                    })
                }
            });



        });


        function conveterParamsToJson(paramsAndValues) {
            var jsonObj = {};

            var param = paramsAndValues.split("&");
            for ( var i = 0; param != null && i < param.length; i++) {
                var para = param[i].split("=");
                jsonObj[para[0]] = para[1];
            }

            return jsonObj;
        }

        /**
         * 将表单数据封装为json
         * @param form
         * @returns
         */
        function getFormData(form) {
            var formValues = $("#" + form).serialize();

            //关于jquery的serialize方法转换空格为+号的解决方法
            formValues = formValues.replace(/\+/g," ");   // g表示对整个字符串中符合条件的都进行替换
            var temp =  decodeURIComponent(JSON.stringify(conveterParamsToJson(formValues)));
            var queryParam = JSON.parse(temp);
            return queryParam;
        }

    </script>
</head>
<body>
<form class="layui-form" action="" id="YuangYuX">
    <input type="hidden" name="saId" id="saId"/>
   <!-- <input type="hidden" name="ids" id="ids">-->
    <div class="layui-form-item">
        <label class="layui-form-label">方案ID</label>
        <div class="layui-input-block">
            <input type="text" name="schemeId" id="schemeId" required
                   lay-verify="required" placeholder="请输入方案ID" autocomplete="off"
                   class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">方案名称</label>
        <div class="layui-input-block" id="fxk">

        </div>
    </div>

   <!-- <div class="layui-form-item">
        <label class="layui-form-label">方案名称</label>
        <div class="layui-input-block">
            <input type="text" name="schemeName" id="schemeName" required
                   lay-verify="required" placeholder="方案名称" autocomplete="off"
                   class="layui-input">
        </div>
    </div>-->

    <div class="layui-form-item">
        <label class="layui-form-label">运动员ID</label>
        <div class="layui-input-block">
            <input type="text" name="athleteId" id="athleteId" required
                   lay-verify="required" placeholder="请输入运动员ID" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

  <!--  <div class="layui-form-item">
        <div id="tp">

        </div>
        <label class="layui-form-label" id="Divimg">头像:</label>
        <div class="layui-input-block">
            <input class="input-file" name="zhaopian" id="zhaopian"
                   type="file">
        </div>
    </div>-->


    <div class="layui-form-item">
        <label class="layui-form-label">运动员姓名</label>
        <div class="layui-input-block">
            <input type="text" name="athleteName" id="athleteName" required
                   lay-verify="required" placeholder="请输入运动员姓名" autocomplete="off"
                   class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">说明,建议</label>
        <div class="layui-input-block">
            <input type="text" name="exp" id="exp" required
                   lay-verify="required" placeholder="请输入说明,建议" autocomplete="off"
                   class="layui-input">
        </div>
    </div>


   <div class="layui-form-item">
        <label class="layui-form-label">是否查看</label>
        <div class="layui-input-block">
            <input type="text" name="isRead" id="isRead" required
                   lay-verify="required" placeholder="请输入是否查看" autocomplete="off"
                   class="layui-input">
        </div>
    </div>


    <!--<div class="layui-form-item">
        <label class="layui-form-label">开关-默认开</label>
        <div class="layui-input-block">
            <input type="checkbox" checked="" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="是|否">
        </div>
    </div>-->



   <!-- <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="text" name="valid" id="valid" required
                   lay-verify="required" placeholder="请输入状态" autocomplete="off"
                   class="layui-input">
        </div>
    </div>-->



    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo" >立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script>


    /**
    * 新增和修改
    */
    var form = null ;
    layui.use('form', function(){
        form = layui.form;
        //监听提交
        form.on('submit(formDemo)', function(data){
            //layer.msg(JSON.stringify(data.field));
            var rolesId = "";
            $("input[name='roles']:checked").each(function(index,item){

                rolesId+=$(this).val()+",";

            });
            $("#ids").val(rolesId);
                alert($("#ids").val());
                var formData = new FormData(document.getElementById("YuangYuX"));
                var url = "../../UserseController/insert";
                $.ajax({
                    url:url,
                    type:"post",
                    data:formData,
                    processData:false,
                    contentType:false,
                    dataType:"json",
                    success:function(obj){
                        alert("成功了");
                        layer.msg(obj.msg, {
                            icon: 1,
                            time: 1000 //1秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            //关闭当前层
                            //当你在iframe页面关闭自身时
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            //刷新父页面
                            //parent.location.reload();
                        });
                    },
                    error:function(e){
                        layer.msg("操作错误!");
                    }
                });
            return false;
        });
        findlx();
        selectjs();
    });

    /**
     * 修改显示
     */
    function findlx(){
        var id = getQueryString("id");
        if(id){
            var url = "../../UserseController/select";
            var data = {saId:id};
            //jquery的ajax方法,查询新闻类型的方法
            $.get(url,data,function(obj){
                //从后台传过来的数据obj$("[value='11']").prop("checked",true);
                    $("#schemeId").val(obj.schemeId);
                    $("#schemeName").val(obj.schemeName);
                    $("#athleteId").val(obj.athleteId);
                    $("#athleteName").val(obj.athleteName);
                    $("#exp").val(obj.exp);
                    $("#isRead").val(obj.isRead);
                //重新渲染
                form.render();
            },"json");
        }

    }


  function selectjs(){

        var url = "../../UserseController/select2";
        var data = {};
        //jquery的ajax方法,查询新闻类型的方法
        $.get(url,data,function(obj){
            //从后台传过来的数据obj$("[value='11']").prop("checked",true)
            $.each(obj,function(index,item){
                $("#fxk").append("<input type='checkbox' name='roles' value='"+item.schemeId+"' title='"+item.schemeName+"'>");
            });
            //重新渲染
            form.render();
        },"json");
    }

</script>
</body>
</html>